package main

import (
	"fmt"
	"net"
)

//简单的tcpserver

func main() {
	fmt.Println("服务器开始启动...")
	listener, err := net.Listen("tcp", "0.0.0.0:30001")
	if err != nil {
		fmt.Println("发生错误：", err)
		return
	}
	defer listener.Close()

	for {
		fmt.Println("等待客户端来连接...")
		conn, err := listener.Accept()
		if err != nil {
			fmt.Println("等待客户端连接异常")
			break
		}
		fmt.Printf("接收到客户端%v的连接 \n", conn.RemoteAddr())
		go processClient(conn)
	}
	fmt.Println("结束服务器监听")

}

func processClient(conn net.Conn) {
	defer conn.Close()
	//循环接收客户端数据
	for {
		readBuf := make([]byte, 1024)
		revLen, err := conn.Read(readBuf)
		if err != nil {
			fmt.Println("接收发生异常：", err)
			return
		}
		fmt.Println("接收到客户端的数据：", string(readBuf[:revLen]))
	}
}
